import time


# 获取基本工商信息
def get_base_info(mongo_instance, soup, cid, company):
    print('start base*******')
    d_company_gsxt_1m = {}
    com_site = soup.find('a', class_="company-link").text if soup.find('a', class_="company-link") else ''  # 公司网址
    com_mobile = ''  # 公司电话
    com_telephone = ''  # 公司手机号
    com_email = ''  # 公司邮箱
    area = ''  # 公司地址
    com_legal = ''  # 公司法人
    if soup.find('div', class_="detail ").findAll('div', class_="f0")[1].findAll('div', class_="in-block"):
        try:
            area = soup.find('div', class_="detail ").findAll('div', class_="f0")[1].findAll('div', class_="in-block")[
                1].text
            area = area.split('：')
            area = area[1]
        except:
            print("read base area error")

    if soup.findAll('span', class_='hidden'):
        try:
            phone = soup.findAll('span', class_='hidden')[0].text
            com_telephone = phone
            phone = phone.replace('[', '')
            phone = phone.replace(']', '')
            phone = phone.replace('"', '')
            com_mobile = phone
        except:
            print("read base phone error")

    if soup.find('span', class_='pl5'):
        com_email = soup.find('span', class_='pl5').find('script').text
        com_email = com_email.replace(']', '')
        com_email = com_email.replace('[', '')
        com_email = com_email.replace('"', '')

    if soup.find('div', class_='humancompany'):
        com_legal = soup.find('div', class_='humancompany').find('div', class_='name').find('a').text
    else:
        com_legal = ''

    if soup.find('div', class_='body -detail modal-scroll'):
        introduce = soup.find('div', class_='body -detail modal-scroll').text
    else:
        introduce = ''

    com_office = area

    try:
        if soup.find('div', class_='logo -w100').find('img'):
            icon = soup.find('div', class_='logo -w100').find('img')['data-src']
    except AttributeError:
        print('no logo')
    create_at = int(time.time())
    update_at = int(time.time())
    # 基础信息
    if soup.find('div', id='_container_baseInfo'):
        if soup.find('div', id='_container_baseInfo').find('table', class_='table -striped-col -border-top-none'):
            trs = soup.find('div', id='_container_baseInfo').find('table',
                                                                  class_='table -striped-col -border-top-none').find_all(
                'tr')

            d_company_gsxt_1m['regist_capital'] = trs[0].find_all('td')[1].find('div')['title']  # 注册资本
            d_company_gsxt_1m['regist_date'] = trs[0].find_all('td')[3].text  # 成立日期,TODO 待优化，目前是不对的

            d_company_gsxt_1m['com_state'] = trs[1].find_all('td')[1].text  # 经营状态
            d_company_gsxt_1m['regist_code'] = trs[1].find_all('td')[3].text  # 工商注册号

            d_company_gsxt_1m['credit_code'] = trs[2].find_all('td')[1].text  # 统一社会信用代码
            d_company_gsxt_1m['org_code'] = trs[2].find_all('td')[3].text  # 组织机构代码

            d_company_gsxt_1m['taxpayer_code'] = trs[3].find_all('td')[1].text  # 纳税人识别号
            d_company_gsxt_1m['com_type'] = trs[3].find_all('td')[3].text  # 公司类型

            d_company_gsxt_1m['end_date'] = trs[4].find_all('td')[1].find('span').text  # 营业期限
            d_company_gsxt_1m['industry'] = trs[4].find_all('td')[3].text  # 行业

            d_company_gsxt_1m['nashui_zizhi'] = trs[5].find_all('td')[1].text  # 纳税人资质
            d_company_gsxt_1m['approve_date'] = trs[5].find_all('td')[3].text  # 核准日期

            d_company_gsxt_1m['shijiao'] = trs[6].find_all('td')[1].text  # 实缴资本
            d_company_gsxt_1m['staff_num'] = trs[6].find_all('td')[3].text  # 人员规模

            d_company_gsxt_1m['canbao_num'] = trs[7].find_all('td')[1].text  # 参保人数
            d_company_gsxt_1m['regist_authority'] = trs[7].find_all('td')[3].text  # 登记机关

            d_company_gsxt_1m['regist_address'] = trs[8].find_all('td')[1].text  # 注册地址
            d_company_gsxt_1m['english_name'] = trs[8].find_all('td')[3].text  # 英文名称

            d_company_gsxt_1m['company_id'] = cid  # 公司id

            d_company_gsxt_1m['company'] = company  # 公司名称
            d_company_gsxt_1m['com_site'] = com_site  # 公司网址
            d_company_gsxt_1m['area'] = area  # 公司地址
            d_company_gsxt_1m['com_mobile'] = com_mobile  # 公司电话
            d_company_gsxt_1m['com_telephone'] = com_telephone  # 公司手机号
            d_company_gsxt_1m['com_email'] = com_email  # 公司邮箱
            d_company_gsxt_1m['com_legal'] = com_legal  # 公司法人
            d_company_gsxt_1m['introduce'] = introduce  # 公司简介,TODO 待完善
            d_company_gsxt_1m['com_office'] = com_office  # 公司办公地址

            d_company_gsxt_1m['icon'] = icon # 公司icon
            d_company_gsxt_1m['create_at'] = create_at # 创建时间
            d_company_gsxt_1m['update_at'] = update_at # 更新时间

            print('success base*******')
            print(d_company_gsxt_1m)
            mongo_instance.base.update({'company_id': cid}, d_company_gsxt_1m, True)
